package common_goroutine

import (
	"gitee.com/xlizy/common-go/utils/zlog"
	"github.com/panjf2000/ants/v2"
	"sync"
	"time"
)

var nonBlockingPoolOnce sync.Once
var nonBlockingPool *ants.Pool

// GetNonBlockingAntsPool 获取非阻塞的ants池
func GetNonBlockingAntsPool() *ants.Pool {
	nonBlockingPoolOnce.Do(func() {
		nonBlockingPool, _ = ants.NewPool(15, ants.WithNonblocking(true), ants.WithMaxBlockingTasks(500), ants.WithExpiryDuration(30*time.Second), ants.WithPanicHandler(func(err interface{}) {
			zlog.Error("非阻塞ants池执行任务异常:{}", err)
		}))
	})
	return nonBlockingPool
}

func GracefulShutdown() {
	if nonBlockingPool != nil {
		nonBlockingPool.Release()
	}
}
